package com.lncg.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 控制台监听器
 * 监听用户按键输入，支持手动同步
 */
@Component
public class ConsoleListener {
    
    @Autowired
    private SyncService syncService;
    
    @Autowired
    private TaskSchedulerService taskSchedulerService;
    
    private volatile boolean running = true;
    private Thread listenerThread;
    
    @PostConstruct
    public void startListening() {
        listenerThread = new Thread(() -> {
            System.out.println("💡 提示: 按 's' 键手动同步，按 'r' 键刷新cron配置，按 'status' 查看状态");
            
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
                while (running) {
                    try {
                        String input = reader.readLine();
                        if (input != null) {
                            input = input.trim().toLowerCase();
                            
                            switch (input) {
                                case "s":
                                    System.out.println("🔄 手动触发同步...");
                                    try {
                                        syncService.syncAll();
                                        System.out.println("✅ 手动同步完成");
                                    } catch (Exception e) {
                                        System.err.println("❌ 手动同步失败: " + e.getMessage());
                                    }
                                    break;
                                    
                                case "r":
                                    System.out.println("🔄 刷新cron配置...");
                                    try {
                                        taskSchedulerService.refreshCronConfiguration();
                                        System.out.println("✅ cron配置刷新完成，当前配置: " + taskSchedulerService.getCurrentCronExpression());
                                    } catch (Exception e) {
                                        System.err.println("❌ cron配置刷新失败: " + e.getMessage());
                                    }
                                    break;
                                    
                                case "status":
                                    System.out.println("📊 任务状态: " + (taskSchedulerService.isTaskRunning() ? "运行中" : "已停止"));
                                    System.out.println("📅 cron配置: " + taskSchedulerService.getCurrentCronExpression());
                                    break;
                                    
                                default:
                                    if (!input.isEmpty()) {
                                        System.out.println("💡 提示: 按 's' 键手动同步，按 'r' 键刷新cron配置，按 'status' 查看状态");
                                    }
                                    break;
                            }
                        }
                    } catch (IOException e) {
                        System.err.println("❌ 读取输入时出错: " + e.getMessage());
                    }
                }
            } catch (IOException e) {
                System.err.println("❌ 控制台监听器启动失败: " + e.getMessage());
            }
        });
        
        listenerThread.setDaemon(true);
        listenerThread.setName("ConsoleListener");
        listenerThread.start();
    }
    
    public void stop() {
        running = false;
        try {
            if (listenerThread != null) {
                listenerThread.interrupt();
            }
        } catch (Exception ignore) {}
    }

    @PreDestroy
    public void onDestroy() {
        stop();
    }
}
